﻿using System;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.Collections;
using System.Drawing;

namespace PropertyGridEx
{
    public class UIComboBox : UITypeEditor
    {
        ListBox box = new ListBox();       
        IWindowsFormsEditorService edSvc = null;

        public UIComboBox()
        {           
            this.box.SelectedIndexChanged += new EventHandler(this.ListBox_SelectedIndexChanged);
            this.box.DrawMode = DrawMode.OwnerDrawFixed;
            this.box.DrawItem += new DrawItemEventHandler(box_DrawItem);

        }

        void box_DrawItem(object sender, DrawItemEventArgs e)
        {
            this.box.ItemHeight = 15;
            using (Graphics g = e.Graphics)
            {
                //如果该项被选择
                Brush myBrush = Brushes.Black;
                if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                    e.Graphics.FillRectangle(new SolidBrush(Color.GreenYellow), e.Bounds);
                else
                    e.Graphics.FillRectangle(new SolidBrush(box.BackColor), e.Bounds);
                //画出项文本
                e.Graphics.DrawString(box.GetItemText(box.Items[e.Index]), e.Font, myBrush, e.Bounds.X, e.Bounds.Y);
                e.DrawFocusRectangle();
            }

           // throw new NotImplementedException();
        }

        private void ListBox_SelectedIndexChanged(object sender,System.EventArgs  e)
        {
            //close 
            if (edSvc != null)
            {
                edSvc.CloseDropDown();
            }
        }

        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context,System.IServiceProvider provider, object value)
        {

            try
            {
                edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                CustomPropertyCollection x = (CustomPropertyCollection)context.Instance;
                if (edSvc != null)
                {
                    if (value is string)
                    {
                        if (box.Items.Count > 0)
                        {
                            box.Items.Clear();
                        }
                       // Hashtable _hash = x[0].ComboBoxList;
                        //foreach (DictionaryEntry myDE in _hash)
                        //{
                        //    //ListItem newItem = new ListItem((string)myDE.Key, (string)myDE.Value);
                        //    //box.Items.Add(newItem);
                        //    box.Items.Add(myDE.Value);
                        //}

                        box.SelectedIndex = box.Items.IndexOf(value);
                       
                        edSvc.DropDownControl(box);
                      
                        return box.Text;

                    }

                }

            }

            catch (Exception ex)
            {

                System.Console.WriteLine("PropertyComboBoxItem Error : " + ex.Message);

                return value;

            }

            return value;

        }

    }

    public class ListItem
    {
        private string id = string.Empty;
        private string name = string.Empty;
        public ListItem(string sid, string sname)
        {
            id = sid;
            name = sname;
        }

        public override string ToString()
        {
            return this.name;
        }
        public string ID
        {
            get
            {
                return this.id;
            }
            set
            {
                this.id = value;
            }
        }
        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }
    }


}
